package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class UpDownListener {

    @Autowired
    private IApArticleService articleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void handleMessage(ConsumerRecord<String, String> record) {
        String key = record.key();
        String value = record.value();
        Long articleId = Long.parseLong(key);
        // 根据文章id更新上下架状态
        LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId, articleId);
        update.set(ApArticle::getIsDown, value.equals("true") ? true : false);
        articleService.update(update);
    }
}
